{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 指定包含jpg文件的目录\n",
    "directory = './txt_label'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "批量重命名完成。\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 初始化文件计数器\n",
    "file_count = 682\n",
    "\n",
    "# 遍历目录中的所有文件\n",
    "for filename in os.listdir(directory):\n",
    "    # 检查文件扩展名是否为.txt\n",
    "    if filename.lower().endswith('.txt'):\n",
    "        # 构造文件的完整路径\n",
    "        old_file = os.path.join(directory, filename)\n",
    "        # 构造新文件名，格式为数字.jpg\n",
    "        new_filename = f\"{file_count}.txt\"\n",
    "        new_file = os.path.join(directory, new_filename)\n",
    "        # 重命名文件\n",
    "        os.rename(old_file, new_file)\n",
    "        # 更新文件计数器\n",
    "        file_count += 1\n",
    "\n",
    "print(\"批量重命名完成。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
